//
//  WRSegmentControl.m
//  WindRunner
//
//  Created by leeqb on 10/6/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "WRSegmentControl.h"

@implementation WRSegmentControl

- (id)initWithItems:(NSArray *)items selectedIndex:(NSInteger)selectedIndex
{
    self = [super init];
    if(self) {
        _segItems = [[NSMutableArray alloc] initWithCapacity:items.count];
        _selectedIndex = selectedIndex;
        _prevSelectedIndex = selectedIndex;
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.backgroundColor = RGB(64, 160, 253);
        
        for(NSString *title in items) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            [button setTitle:title forState:UIControlStateNormal];
            [button addTarget:self action:@selector(segButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_segItems addObject:button];
            [self addSubview:button];
        }
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat avgWidth = self.frame.size.width / _segItems.count;
    CGRect rect = CGRectZero;
    for(NSInteger i=0; i<_segItems.count; i++) {
        UIButton *button = _segItems[i];
        button.tag = i+1;
        if(i > 0) {
            rect.origin.x += i*avgWidth + 0.5;
        } else {
            rect.origin.x += i*avgWidth;
        }
        rect.origin.y = 0;
        rect.size.width = avgWidth;
        rect.size.height = self.frame.size.height;
        button.frame = rect;
        if(i == _selectedIndex) {
            [button setBackgroundImage:[UIImage imageWithColor:RGB(64, 160, 253) size:button.frame.size] forState:UIControlStateNormal];
        } else {
            [button setTitleColor:RGB(64, 160, 253) forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageWithColor:RGB(134, 211, 255) size:button.frame.size] forState:UIControlStateNormal];
        }
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    // 点击同一按钮不做处理
    if(selectedIndex == _prevSelectedIndex) {
        return;
    }
    
    _selectedIndex = selectedIndex;
    _prevSelectedIndex = selectedIndex;
    
    // 将所有按钮置为非选中状态
    for(UIButton *btn in _segItems) {
        [btn setTitleColor:RGB(64, 160, 253) forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:RGB(134, 211, 255) size:btn.frame.size] forState:UIControlStateNormal];
    }
    
    if(selectedIndex >= 0) {
        // 设置当前点击按钮的背景颜色为深色
        UIButton *currButton = (UIButton *)[self viewWithTag:selectedIndex+1];
        [currButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [currButton setBackgroundImage:[UIImage imageWithColor:RGB(64, 160, 253) size:currButton.frame.size] forState:UIControlStateNormal];
    }
}

- (void)segButtonClick:(UIButton *)button
{
    // 点击同一按钮不做处理
    NSInteger currSelectedIndex = button.tag - 1;
    if(currSelectedIndex == _prevSelectedIndex) {
        return;
    }
    
    // 执行setSelectedIndex函数
    self.selectedIndex = currSelectedIndex;
    if(self.delegate && [self.delegate respondsToSelector:@selector(segmentChanged:)]) {
        [self.delegate segmentChanged:currSelectedIndex];
    }
}

@end
